<template>
  <div class="backStack-issue-wrap informationCreate">
    <From
      ref="formRef"
      layoutType="grid"
      :isErrorMessage="false"
      :optionList="formRows"
      :formRules="formRules"
      :defaultModel="defaultModel"
    >
    </From>
    <div class="backStack-issue-footer-wrap">
      <div class="submit-wrap">
        <a-button class="submit-audit" @click="toAudit">保存并发布</a-button>
      </div>
    </div>
  </div>
</template>

<script>
import From from '../../components/formTable/form.vue'
import mixinsBackStack from '@/mixins/backStack'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinFakeData from '@/mixins/makeFakerData'
import { formRules, formRows, counseling, policy } from './configs/formOptions.js'
import { getContentPolicyDetail, editContentPolicy } from '@/api/contentPolicy.js'

export default {
  mixins: [mixinsBackStack, mixinConfigSetting, mixinFakeData],
  data() {
    return {
      loading: true,
      formRows, // 表单item配置
      formRules, // 表单校验规则配置
      counseling,
      policy,
      defaultModel: {},
      detailId: null,
      isRead: false
    }
  },
  components: {
    From
  },
  created() {
    this.initSetting()
  },
  methods: {
    // 初始化数据
    initSetting() {
      const { id } = this.$route.query
      this.detailId = id
      this.initDefaultModel()
    },
    // 获取默认值
    async initDefaultModel() {
      const { data } = await getContentPolicyDetail(this.detailId)
      this.handleResult(data)
    },
    handleResult(instance) {
      let { annexes, firstPic, ...otherParams } = instance
      annexes = annexes && annexes[0] ? annexes : [] //兼容数据[null]
      this.defaultModel = {
        ...otherParams,
        annexIds: annexes,
        pic: [firstPic]
      }
    },
    readChange(e) {
      const { target } = e
      this.isRead = target.checked
    },
    // 参数处理
    beforeRequest() {
      const { detailId } = this
      const { title, type, pulishDate, source, ...formValue } = this.$refs.formRef.formModel
      const annexIds = this.handlerAnnexIds(formValue).join(',')
      const picPath = this.handlerPicPathIds(formValue).join(',')
      const params = { title, type, pulishDate, id: detailId, annexIds, pic: picPath, source }
      return params
    },
    handlerAnnexIds(instance) {
      const { annexIds } = instance
      if (!annexIds && annexIds[0]) return []
      return annexIds.reduce((pre, item) => {
        if (item.response) {
          const { fileId } = item.response.data[0]
          pre.push(fileId)
        } else {
          pre.push(item.id)
        }
        return pre
      }, [])
    },
    handlerPicPathIds(picPath) {
      const { pic } = picPath
      if (!pic && pic[0]) return []
      return pic.reduce((pre, item) => {
        if (item.response) {
          const { fileId } = item.response.data[0]
          pre.push(fileId)
        } else {
          pre.push(item.id)
        }
        return pre
      }, [])
    },
    async toAudit() {
      const v = this.$refs.formRef.validatedForm()
      if (!v) return
      const params = this.beforeRequest()
      params.status = '1'
      const { msg, code } = await editContentPolicy(params)
      this.$apiStatusHandler(code, msg)
      this.$router.push({ name: 'contentPolicy' })
    }
  }
}
</script>
